package com.atguigu.eduService.controller.front;

import com.atguigu.commonUtils.JwtUtils;
import com.atguigu.commonUtils.Result;
import com.atguigu.commonUtils.ResultEnum;
import com.atguigu.commonUtils.ResultUtil;
import com.atguigu.commonUtils.vo.CourseVo;
import com.atguigu.eduService.client.OrderClient;
import com.atguigu.eduService.entity.EduChapter;
import com.atguigu.eduService.entity.EduCourse;
import com.atguigu.eduService.entity.EduTeacher;
import com.atguigu.eduService.service.EduChapterService;
import com.atguigu.eduService.service.EduCourseService;
import com.atguigu.eduService.service.EduTeacherService;
import com.atguigu.eduService.vo.CourseFrontVo;
import com.atguigu.eduService.vo.CourseWebVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "前台课程模块")
@RestController
@RequestMapping("/eduService/courseFront")
@CrossOrigin
public class CourseFrontController {


    @Resource
    private EduCourseService eduCourseService;
    @Resource
    private EduChapterService eduChapterService;
    @Resource
    private OrderClient orderClient;

    @ApiOperation("条件查询带分页")
    @PostMapping("/getCourseFrontList/{page}/{limit}")
    public Result<Map<String, Object>> getCourseFrontList(@PathVariable Long page, @PathVariable Long limit,
                                                          @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Map<String, Object> eduCourseMap = eduCourseService.getCourseFrontList(page, limit, courseFrontVo);
        return ResultUtil.success(eduCourseMap);
    }

    @ApiOperation("查询课程详情")
    @GetMapping("/getCourseFrontInfo/{courseId}")
    public Result<Map<String, Object>> getCourseFrontInfo(@PathVariable String courseId, HttpServletRequest request){
        Map<String, Object> map = new HashMap<>();
        CourseWebVo courseWebVo = eduCourseService.getCourseFrontInfo(courseId);
        List<EduChapter> chapterVideoList = eduChapterService.getChapterVideo(courseId);
        //根据课程id和用户id查询订单状态
        String id = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(id)) {
            return ResultUtil.defineSuccess(ResultEnum.INVALID_TOKEN.code,null,"未登录");
        }
        Result<Boolean> result = orderClient.isBuyCourse(courseId, id);
        map.put("courseWebVo",courseWebVo);
        map.put("chapterVideoList",chapterVideoList);
        map.put("isBuy",result);
        return ResultUtil.success(map);
    }

    @ApiOperation("根据课程id查询课程信息")
    @GetMapping("/getCourseInfo/{courseId}")
    public CourseVo getCourseInfo(@PathVariable String courseId){
        CourseWebVo courseWebVo = eduCourseService.getCourseFrontInfo(courseId);
        CourseVo courseVo = new CourseVo();
        BeanUtils.copyProperties(courseWebVo,courseVo);
        return courseVo;
    }


}
